package com.easepal802s.project.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.ogawa.base.Constant.BleConstant;
import com.easepal802s.project.R;
import com.easepal802s.project.ble.BleController;
import com.easepal802s.project.ui.iview.IFraMassageView;
import com.easepal802s.project.ui.presenter.FraMassagePresenter;
import com.easepal802s.project.view.LongClickImageView;
import com.easepal802s.project.view.LongClickImageView2;
import com.easepal802s.project.view.MoveLayout;
import com.easepal802s.project.widget.LayoutFunctionView;
import com.easepal802s.project.widget.LayoutGears5;
import com.easepal802s.project.widget.LayoutGears6;
import com.ogawa.base.utils.CommonUtil;
import com.umeng.analytics.MobclickAgent;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;


/**按摩手法
 * Created by LCH on 2018/1/29.
 */
public class MassageTechniqueFragment extends Fragment implements LongClickImageView2.onDownAction,View.OnClickListener,IFraMassageView,MoveLayout.OnChooseAction{


    private View inflate;

    private RelativeLayout mLl3D;
    private RelativeLayout mLlDynamics;//力度
    private RelativeLayout mLlSpeed;
    private LinearLayout mLlSelectSkill;//选择手法
    private TextView tvTime;

    private TextView mTvFix;//定点，区间 全身
    private int flag_area = 0;//0 mTvFix 关闭（默认定点），1 定点开 2 区间 3 全身
    private int[] mRnImage = {R.mipmap.knead0,R.mipmap.knead1,R.mipmap.knead2};//揉捏
    private int[] mQJImage = {R.mipmap.tap0,R.mipmap.tap1,R.mipmap.tap2};//敲击
    private int[] mRDSImage = {R.mipmap.swedish0,R.mipmap.swedish1,R.mipmap.swedish2};//瑞典式
    private int[] mQRImage = {R.mipmap.knead_tap0,R.mipmap.knead_tap1,R.mipmap.knead_tap2};//敲揉
    private int[] mZYImage = {R.mipmap.shiatsu0,R.mipmap.shiatsu1,R.mipmap.shiatsu2};//指压
    private int[] mTNImage = {R.mipmap.roll0,R.mipmap.roll1};//指压
    private List<int[]> mImages = new ArrayList<>();


    private LongClickImageView2 mHeadup;//up
    private  LongClickImageView2  mHeadDown;//DOWN
    private  FrameLayout mFlwidth;//宽度背景
    private LongClickImageView mIvLeft,mIvRight;//宽度加减
    private ImageView mIvRN;//揉捏
    private ImageView mIvQJ;//敲击
    private ImageView mIvRDS;//瑞典式
    private ImageView mIvQR;//敲揉
    private ImageView mIvZY;//指压
    private ImageView mIvTN;//推拿
    private List<ImageView> mIvSkills = new ArrayList<>();
    private String[] name = {"3D","力度","速度","手法"};

    private LayoutFunctionView mLayoutFunctionView;

    private LongClickImageView mIvAdd3D;  //3D+
    private LongClickImageView mIvSub3D; //3D-
    private LongClickImageView mIvAddPower;//力度
    private LongClickImageView mIvSubPower;
    private LongClickImageView mIvAddSpeed;  //速度
    private LongClickImageView mIvSubSpeed; //速度
    private LayoutGears6 mLayout3D;
    private LayoutGears5 mLayoutPower;
    private LayoutGears5 mLayoutSpeed;
    private FraMassagePresenter mPresenter;
    private MyHandler myHandler;
    private MoveLayout mLayout;
    private ScrollView mScroll;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        inflate = inflater.inflate(R.layout.fragment_massage_technique, container, false);
        initView();
        return inflate;
    }
    public void setTab(int tab){
        if(mLayout!=null){
            Log.e("shunxu","setTab"+tab);
            mLayout.setTabSelect(tab);
            mLl3D.setVisibility(View.GONE);
            mLlSelectSkill.setVisibility(View.VISIBLE);
            mLlSpeed.setVisibility(View.GONE);
            mLlDynamics.setVisibility(View.GONE);
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getName());
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getName());
    }

    private void initView() {
        Log.e("shunxu","initView");
        mPresenter = new FraMassagePresenter(this);
        myHandler = new MyHandler(this);

        mHeadup  = inflate.findViewById(R.id.headup);
        mHeadDown= inflate.findViewById(R.id.headdown);
        mScroll =  inflate.findViewById(R.id.massage_fragment_scroll);
        mLayout=  inflate.findViewById(R.id.massege_fragment_move_layout);
        mLayout.setData(name);
        mLayout.setScollView(mScroll);
        mLayout.setAction(this);

        mHeadup.setScollView(mScroll);
        mHeadup.setCommondAction(BleConstant.JIXINUP,this);
        mHeadup.intiListener();
//        mHeadup.setOnClickListener(this);

        mHeadDown.setScollView(mScroll);
        mHeadDown.setCommondAction(BleConstant.JIXINDOWM,this);
        mHeadDown.intiListener();
//        mHeadDown.setOnClickListener(this);
        mIvLeft  =  inflate.findViewById(R.id.iv_width_left);
        mIvLeft.intiListener();
        mIvRight =  inflate.findViewById(R.id.iv_width_right);
        mIvRight.intiListener();
        mFlwidth = inflate.findViewById(R.id.fragment_mt_fl_width);
        tvTime   = inflate.findViewById(R.id.tvTime);

        mIvRN = inflate.findViewById(R.id.fragment_mt_tv_knead);
        mIvQJ = inflate.findViewById(R.id.fragment_mt_tv_tap);
        mIvRDS = inflate.findViewById(R.id.fragment_mt_tv_swedish);
        mIvQR = inflate.findViewById(R.id.fragment_mt_tv_knead_tap);
        mIvZY = inflate.findViewById(R.id.fragment_mt_tv_shiatsu);
        mIvTN = inflate.findViewById(R.id.fragment_mt_tv_roll);
        mImages.clear();
        mIvSkills.clear();
        mImages.add(mRnImage);
        mImages.add(mQJImage);
        mImages.add(mRDSImage);
        mImages.add(mQRImage);
        mImages.add(mZYImage);
        mImages.add(mTNImage);

        mIvSkills.add(mIvRN);
        mIvSkills.add(mIvQJ);
        mIvSkills.add(mIvRDS);
        mIvSkills.add(mIvQR);
        mIvSkills.add(mIvZY);
        mIvSkills.add(mIvTN);


        mLayout3D    = inflate.findViewById(R.id.layout_3d);
        mLayoutPower = inflate.findViewById(R.id.linear_air_massage_strong);
        mLayoutSpeed = inflate.findViewById(R.id.linear_air_massage_speed);

        mIvAdd3D = inflate.findViewById(R.id.iv_gears6_add);
        mIvAdd3D.intiListener();
        mIvSub3D = inflate.findViewById(R.id.iv_gears6_sub);
        mIvSub3D.intiListener();
        mIvAddPower = mLayoutPower.findViewById(R.id.iv_gears5_add);
        mIvAddPower.intiListener();
        mIvSubPower = mLayoutPower.findViewById(R.id.iv_gears5_sub);
        mIvSubPower.intiListener();
        mIvAddSpeed = mLayoutSpeed.findViewById(R.id.iv_gears5_add);
        mIvAddSpeed.intiListener();
        mIvSubSpeed = mLayoutSpeed.findViewById(R.id.iv_gears5_sub);
        mIvSubSpeed.intiListener();


        mTvFix= inflate.findViewById(R.id.fragmentFixpoint);
        mTvFix.setOnClickListener(this);
        mLayoutFunctionView = inflate.findViewById(R.id.fragment_massage_layout_function);



        mLl3D = inflate.findViewById(R.id.layout_3d);
        mLlDynamics = inflate.findViewById(R.id.linear_air_massage_strong);
        mLlSpeed = inflate.findViewById(R.id.linear_air_massage_speed);
        mLlSelectSkill= inflate.findViewById(R.id.linear_methods_to_select);


        mIvAdd3D.setOnClickListener(this);
        mIvSub3D.setOnClickListener(this);



        mIvLeft.setOnClickListener(this);
        mIvRight.setOnClickListener(this);

        mIvRN.setOnClickListener(this);
        mIvQJ.setOnClickListener(this);
        mIvRDS .setOnClickListener(this);
        mIvQR.setOnClickListener(this);
        mIvZY.setOnClickListener(this);
        mIvTN.setOnClickListener(this);
        tvTime.setOnClickListener(this);

        mIvAddSpeed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.SUDUADD, true);
                }
            }
        });
        mIvSubSpeed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.SUDUSUB, true);
                }
            }
        });
        mIvAddPower.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.LIDUADD, true);
                }
            }
        });
        mIvSubPower.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.LIDUSUB, true);
                }
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tvTime:
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.TIME, true);
                }

                break;
            case R.id.fragmentFixpoint:
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.DINGDQUJ, true);
                }

                break;
//            case R.id.headup:
//                if(mScroll!=null){
//                    mScroll.requestDisallowInterceptTouchEvent(true);
//                }
//                if (CommonUtil.isFastClick()) {
//                    return;
//                } else {
//                    BleController.getInst().sendLongclickSingleData(BleConstant.JIXINUP,true);
//                }
//
//
//                break;
//            case R.id.headdown:
//                if(mScroll!=null){
//                    mScroll.requestDisallowInterceptTouchEvent(true);
//                }
//                if (CommonUtil.isFastClick()) {
//                    return;
//                } else {
//                    BleController.getInst().sendLongclickSingleData(BleConstant.JIXINDOWM,true);
//                }
//                break;
            case R.id.iv_width_left:
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.KUANDUSUB, true);
                }
                break;
            case R.id.iv_width_right:
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.KUANDUADD, true);
                }
                break;
            case R.id.iv_gears6_add:
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.JIX3DADD, true);
                }
                break;
            case R.id.iv_gears6_sub:
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.JIX3DSUB, true);
                }
                break;

            case R.id.fragment_mt_tv_knead:
                //揉捏
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.ROUN, true);
                }
                break;
            case R.id.fragment_mt_tv_tap:
                //敲击
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.QIAOJ, true);
                }
                break;
            case R.id.fragment_mt_tv_swedish:
                //瑞典式
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.RUID, true);
                }
                break;
            case R.id.fragment_mt_tv_knead_tap:
                //敲揉
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.QIAOR, true);
                }
                break;
            case R.id.fragment_mt_tv_shiatsu:
                //指压
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.ZHIYA, true);
                }
                break;
            case R.id.fragment_mt_tv_roll:
                //推拿
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.TUINA, true);
                }

                break;

        }
    }





    @Override
    public void onDestroy() {
        super.onDestroy();
        mLayoutFunctionView.onDestory();
        mPresenter.OnDestory();
    }

    @Override
    public void getWidth(int position) {
        myHandler.obtainMessage(1,position,-1).sendToTarget();

    }
    private void setWidth(int position){
        if(position==0){
            mFlwidth.setBackgroundResource(R.mipmap.width0);
        }
        else if(position ==1){
            mFlwidth.setBackgroundResource(R.mipmap.width1);
        }
        else if(position ==2){
            mFlwidth.setBackgroundResource(R.mipmap.width2);
        }
        else if(position ==3){
            mFlwidth.setBackgroundResource(R.mipmap.width3);
        }
        else {
            mFlwidth.setBackgroundResource(R.mipmap.width0);
        }

    }

    @Override
    public void getArea(int position) {
        myHandler.obtainMessage(2,position,-1).sendToTarget();
    }
    private void setArea(int position){
        if(position ==0){
            mTvFix.setSelected(false);
            mTvFix.setText("定点");
        }
       else   if(position ==1 ){
            mTvFix.setSelected(true);
            mTvFix.setText("定点");
        }
        else if(position ==2){
            mTvFix.setSelected(true);
            mTvFix.setText("区间");
        }

    }

    @Override
    public void getTime(int min, int sec) {
        myHandler.obtainMessage(3,min,sec).sendToTarget();
    }


    @Override
    public void get3D(int position) {
        myHandler.obtainMessage(4,position,-1).sendToTarget();
    }

    @Override
    public void getPower(int position) {
        myHandler.obtainMessage(5,position,-1).sendToTarget();
    }

    @Override
    public void getSpeed(int position) {
        myHandler.obtainMessage(6,position,-1).sendToTarget();
    }

    @Override
    public void getSkill(int position, int stronger) {
        myHandler.obtainMessage(7,position,stronger).sendToTarget();
    }

    /**
     *
     * @param position 0揉捏，1敲击，2瑞典式，3敲揉，4指压，5推拿；
     * @param stronger 强度
     */
    private synchronized void setSkill(int position, int stronger){
        mIvSkills.get(position).setImageResource(mImages.get(position)[stronger]);
    }

    @Override
    public void onChoose(int position) {
        mLayout.setTectColor(position);
        switch (position){
            case 0:
                mLl3D.setVisibility(View.VISIBLE);
                mLlSelectSkill.setVisibility(View.GONE);
                mLlSpeed.setVisibility(View.GONE);
                mLlDynamics.setVisibility(View.GONE);
                break;
            case 1:
                mLl3D.setVisibility(View.GONE);
                mLlDynamics.setVisibility(View.VISIBLE);
                mLlSelectSkill.setVisibility(View.GONE);
                mLlSpeed.setVisibility(View.GONE);
                break;
            case 2:
                mLl3D.setVisibility(View.GONE);
                mLlDynamics.setVisibility(View.GONE);
                mLlSpeed.setVisibility(View.VISIBLE);
                mLlSelectSkill.setVisibility(View.GONE);
                break;
            case 3:
                mLl3D.setVisibility(View.GONE);
                mLlDynamics.setVisibility(View.GONE);
                mLlSpeed.setVisibility(View.GONE);
                mLlSelectSkill.setVisibility(View.VISIBLE);
                break;
        }

    }

    @Override
    public void onDownClick(String what) {
        Log.e("LongNext","onDownClick");
        if (CommonUtil.isFastClick()) {
            return;
        } else {
            BleController.getInst().sendLongclickSingleData(what,true);
        }
    }


    private class MyHandler extends Handler {
        WeakReference<MassageTechniqueFragment> mMassageTechniqueFragmentWeakReference;

        MyHandler(MassageTechniqueFragment mMassageTechniqueFragment) {
            mMassageTechniqueFragmentWeakReference = new WeakReference<>(mMassageTechniqueFragment);
        }

        @Override
        public void handleMessage(Message msg) {
            final MassageTechniqueFragment mMassageTechniqueFragment = mMassageTechniqueFragmentWeakReference.get();
            if (mMassageTechniqueFragment != null) {
                switch (msg.what) {
                    case 1:
                        //宽度
                        setWidth(msg.arg1);
                        break;
                    case 2:
                        //定点
                       setArea(msg.arg1);
                        break;
                    case 3:
                        //时间
                        String min = String.valueOf(msg.arg1);
                        String sec = String.valueOf(msg.arg2);
                        if(min.length()==1){
                            min = "0"+min;
                        }
                        if(sec.length()==1){
                            sec = "0"+sec;
                        }
                        tvTime.setText(min + " : " + sec);

                        break;
                    case 4:
                        //3d
                        mLayout3D.setShow(msg.arg1);
                        break;
                    case 5:
                        //力度
                        mLayoutPower.setShow(msg.arg1);
                        break;
                    case 6:
                        //速度
                        mLayoutSpeed.setShow(msg.arg1);
                        break;
                    case 7:
                        //手法
                        setSkill(msg.arg1,msg.arg2);
                        break;

                }
            }
        }
    }
}
